package com.face.server.repository.exRepository;

import com.face.server.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ryan on 2018/1/25.
 */
@Repository
public class exStudentRepositoryImpl implements exStudentRepository {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public List<Student> listStudents(PageRequest pageRequest, String key1, String key2) {
        String[] keys1=new String[]{"name","studentNumber"};
        String[] keys2=new String[]{"dormitoryBuildNum","dormitoryNum","sex"};

        Criteria integ=new Criteria();
        ArrayList<Criteria> temp = new ArrayList<>();

        Criteria criteria=new Criteria();
        List<Criteria> criteriaList=new ArrayList<Criteria>();
        Criteria criteria1=new Criteria();
        criteria1.orOperator(Arrays.stream(keys1).map(e->Criteria.where(e).regex(key1)).toArray(s->new Criteria[s]));
        Criteria criteria2=new Criteria();
        criteria2.orOperator(Arrays.stream(keys2).map(e->Criteria.where(e).is(Integer.parseInt(key1))).toArray(s->new Criteria[s]));
        criteriaList.add(criteria1);criteriaList.add(criteria2);
        criteria.orOperator(criteriaList.stream().toArray(s->new Criteria[s]));
        temp.add(criteria);

        if (key2!=null&&key2.equals("")==false){
            List<Criteria> criList=new ArrayList<Criteria>();
            Criteria cri=new Criteria();
            Criteria cri1=new Criteria();
            cri1.orOperator(Arrays.stream(keys1).map(e->Criteria.where(e).regex(key2)).toArray(s->new Criteria[s]));
            Criteria cri2=new Criteria();
            cri2.orOperator(Arrays.stream(keys2).map(e->Criteria.where(e).is(Integer.parseInt(key2))).toArray(s->new Criteria[s]));
            criList.add(cri1);
            criList.add(cri2);
            cri.orOperator(criList.stream().toArray(s->new Criteria[s]));
            temp.add(cri);
        }
        integ.andOperator(temp.stream().toArray(s->new Criteria[s]));

        int page=pageRequest.getPageNumber()>0?pageRequest.getPageNumber()*pageRequest.getPageSize():0;
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(integ),
                Aggregation.sort(Sort.Direction.ASC,"dormitoryBuildNum"),
                Aggregation.skip((long)page),
                Aggregation.limit(pageRequest.getPageSize())
        );
        AggregationResults<Student> aggrResult = mongoTemplate.aggregate(aggregation, "student", Student.class);
        return aggrResult.getMappedResults();
    }

    @Override
    public void deleteStudents() {
        LocalDate localDate=LocalDate.now();
        Criteria criteria=Criteria.where("endDate").gt(localDate);
        Query query=Query.query(criteria);
        mongoTemplate.remove(query,Student.class);
        return;
    }
}
